package com.axdoc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.util.excel.ExcelUtil;

import axdoc.dao.TagsDao;
import axdoc.model.tags.ExcelModel;
import axdoc.model.tags.TagsDisease;
import axdoc.model.tags.TagsDiseaseDept;
import axdoc.model.tags.TagsDiseaseIllness;
import axdoc.model.tags.TagsIllness;
import axdoc.model.tags.TagsIllnessSymptom;
import axdoc.model.tags.TagsSymptom;
import axdoc.util.StringUtils;

public class TagsUser20180131 {
	
	
	@Autowired
	TagsDao tagsDao;

	@Autowired
	JdbcTemplate jdbcTemplate;
	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
		TagsDao tagsDao=ac.getBean(TagsDao.class);
		List<Object> list = new ArrayList<Object>();
		
		
		
		for (int i = 1; i <= 7; i++) {
			System.out.println("文件No:---------------------"+i);
			String path1 = "/Users/sunzhaoli/Excels/userTags0"+i+".xlsx";
			List<Object> list1 = ExcelUtil.getInstance().readExcel2ObjsByPath(path1, ExcelModel.class);
			list.addAll(list1);
			
		}
		
		
		
		
		
		if (list != null && list.size() > 0) {
			Map<String, Integer> diseaseMap = new HashMap<String, Integer>();
			Map<String, Integer> deptMap = new HashMap<String, Integer>();
			int i = 0;
			for (Object o : list) {
				i++;
				System.out.println(i + "==" + o.toString());

				ExcelModel em = (ExcelModel) o;

				// 新增病种
				Integer diseaseId = diseaseMap.get(em.getDisease().trim());
				if (diseaseId == null) {
					TagsDisease tc = new TagsDisease();
					tc.setDiseaseName(em.getDisease().trim());
					diseaseId = tagsDao.addDisease(tc);
					diseaseMap.put(em.getDisease().trim(), diseaseId);
					System.out.println("disId=" + diseaseId);
				}

				// 新增病种科室关联
				String[] depts = em.getDept().trim().replace("，", ",").split(",");
				for (String dept : depts) {
					Integer deptId = deptMap.get(dept.trim() + diseaseId);
					if (deptId == null) {
						TagsDiseaseDept tdd = new TagsDiseaseDept();
						tdd.setDiseaseId(diseaseId);
						tdd.setDept(dept);
						deptId = tagsDao.addDiseaseDept(tdd);
						deptMap.put(dept.trim() + diseaseId, deptId);
						System.out.println("deptId=" + deptId);
					}
				}
				if(StringUtils.isNotEmpty(em.getIllness())) {
					TagsIllness ti = new TagsIllness();
					ti.setIllnessName(em.getIllness()==null?"":em.getIllness().trim());
					ti.setIllnessDesc(em.getDesc()==null?"":em.getDesc().trim());
					int illnessId = tagsDao.addIllness(ti);
					System.out.println("illnessId=" + illnessId);

					TagsDiseaseIllness tdi = new TagsDiseaseIllness();
					tdi.setDiseaseId(diseaseId);
					tdi.setIllnessId(illnessId);
					int disIllId = tagsDao.addDiseaseIllness(tdi);
					System.out.println("disIllId=" + disIllId);
					if(em.getSyms()!=null) {
						String[] syms = em.getSyms().trim().replace("，", ",").split(",");
						if (syms != null && syms.length > 0) {
							for (int j = 0; j < syms.length; j++) {
								if (StringUtils.isNotBlank(syms[j])) {
									TagsSymptom ts = new TagsSymptom();
									ts.setSymptomName(syms[j].trim());
									int symId = tagsDao.addSymptom(ts);
									System.out.println("symId=" + symId);

									TagsIllnessSymptom tis = new TagsIllnessSymptom();
									tis.setIllnessId(illnessId);
									tis.setSymptomId(symId);
									int illSymId = tagsDao.addIllnessSymptom(tis);
									System.out.println("illSymId=" + illSymId);
								}
							}
						}
					}
				}
				
				

			}
		}
	
	}
}
